<template>
	<view class="container">
		<navBar title="每日工作任务单" @goBack="uni.navigateBack()"></navBar>
		

		<!-- #ifdef APP-PLUS || H5 -->
		<view class="conditional-acceptance-list" :style="{height: `calc(100% - ${systemInfo.navbarHeight || 0}px - ${systemInfo.stateHeight || 0}px - ${systemInfo.bottomHeight || 0}px)`}">
		<!-- #endif -->
		<!--  #ifdef MP-WEIXIN -->
		<view class="conditional-acceptance-list" :style="{height: `calc(100% - ${systemInfo.bottomHeight || 0}px)`}">
		<!-- #endif -->
			<view class="tab">
				<view @click="changeTabIndex(0)" :class="[tabIndex === 0 ? 'active' : '']">待处理</view>
				<view @click="changeTabIndex(1)" :class="[tabIndex === 1 ? 'active' : '']">任务单记录</view>
			</view>
			
			<view class="search-content">
				<view class="search">
					<u-icon name="search" color="#a7aebd" size="26"></u-icon>
					 <u--input @change="search" v-model="keyword" placeholder="请输入施工部位、项目名、桩号、填报人" border="surround" :clearable="false" :customStyle="{border: 'none'}"></u--input>
				</view>
				<view v-if="tabIndex === 0" class="option">
					<view @click="fnAdd" style="color:#2F80ED;">新建任务单</view>
				</view>
			</view>
			
			<scroll-view @scrolltolower="scrolltolower" class="scroll-view" scroll-y>
				<view>
					<view v-for="(item, index) in constructionPlanList" :key="index" class="item-content" @click.stop="editTaskPage(item)">
						<view class="title">
							<view class="text-overflow">{{ item.constructionSite }}</view>
							<view :class="[stateClass(item.taskState)]" class="state">{{ item.codeName }}</view>
							
						</view>
						<view class="date">
							<u-icon name="clock" color="#6a819e" size="14"></u-icon>
							<view class="date-text">创建时间：{{  getDate2(item.recordCreateDate) }} | {{ item.submitUserName }}</view>
						</view>
						<commonLookImage :imageArr="item.urls2" width="80px" height="80px"></commonLookImage>
					</view>
					<u-empty v-if="!constructionPlanList.length" mode="data" text="数据为空" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
				</view>
			</scroll-view>
		</view>	
		
	</view>
</template>

<script>
	import { handleFormatDate } from "@/utils/date.js"
	import { getSystemHeightInfo } from "@/utils/common.js"
	import { mjsDailyMission_list } from "@/api/mjs.js"
	import mixins from "@/utils/mixins.js"
	export default {
		mixins:[mixins],
		data() {
			return {
				tabIndex: 0,
				keyword: '',
				paging: {
					pageNum: 1,
					pageSize: 10,
					total: 0
				},
				constructionPlanList: [],
				stateStr: {
					'primed_for_action': '待发起',
					'pending_order': '待接单',
					'ordered': '已派单',
					'finished': '已完成',
				},
				currentTypes: [],
				currentTypeStr: '',
				systemInfo: {},
				code: '',
			}
		},
		onLoad(options) {
			this.code = options.code
			this.systemInfo = getSystemHeightInfo()
			uni.$off()
			uni.$on('refreshData', data => {
				console.log(data)
				if(data.isUpdate) {
					this.tabIndex = data.index
					this.search()
				}
			})
		},
		mounted() {
			this.search()
		},
		methods: {
			changeTabIndex(index) {
				if (this.tabIndex === index) {
					return
				}
				this.tabIndex = index
				this.search()
			},
			scrolltolower() {
				if (this.paging.pageNum * this.paging.pageSize < this.paging.total) {
					this.paging.pageNum++
					this.getConstructionPlanArray()
				}
			},
			search(val) {
				if(!val) {
					this.keyword = ''
				}
				this.constructionPlanList = []
				this.paging.pageNum = 1
				this.getConstructionPlanArray()
			},
			getConstructionPlanArray() {
				const { pageNum, pageSize } = this.paging
				mjsDailyMission_list({
					currPage: pageNum,
					pageSize,
					params: {
						type: this.tabIndex == 0 ? 'dcl' : 'jl',
						keyword: this.keyword,
						projectId: uni.getStorageSync("isOwner"),
						taskStates:this.tabIndex == 0 ? ['primed_for_action', 'pending_order'] : ["ordered", "finished"]
					}
				}).then(res => {
					console.log(res, '待处理')
					if (res && res.result && res.result.list) {
						this.constructionPlanList = this.constructionPlanList.concat(res.result.list.map(it => ({
							...it,
							codeName:this.stateStr[it.taskState],
							urls2:(it.constructionDrawingAttach && it.constructionDrawingAttach.length) ? 
									it.constructionDrawingAttach.map(item => ({
										url:this.$config.IMG_FILE_URL + item.fileName,
									})) : []
						})))
						console.log(this.constructionPlanList,'列表')
						this.paging.total = res.result.total
					}
				})
			},
			stateClass(state) {
				if (this.stateStr[state] === '待发起') {
					return 'orange'
				}
				else if (this.stateStr[state] === '待接单') {
					return 'light-blue'
				}
				else if (this.stateStr[state] === '已派单') {
					return 'red'
				}
				else if (this.stateStr[state] === '已完成') {
					return 'green'
				}
			},
			fnAdd() {
				uni.navigateTo({
					url:`/subpackage-mjs/pages/dailyTaskList/addTask`
				})
			},
			editTaskPage(item) {
				let type ='add'
				if(item.taskState === 'primed_for_action') {
					type = 'edit'
				}else if(item.taskState === 'pending_order') {
					type = 'ordered'
				}else if(item.taskState === 'ordered') {
					type = 'delete'
				}else if(item.taskState === 'finished') {
					type = 'finished'
				}
				uni.navigateTo({
					url:`/subpackage-mjs/pages/dailyTaskList/addTask?type=${type}&id=${item.id}`,
					// success: res => {
					// 	res.eventChannel.emit('sendMaskData', {
					// 		data: item.id
					// 	})
					// }
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		box-sizing: border-box;
		height: 100vh;
		overflow: hidden;
		/* #ifdef MP-WEIXIN */
		padding-top: 200rpx;
		/* #endif */
	}
	.conditional-acceptance-list {
		box-sizing: border-box;
		border-top: 15px solid #f5f7fa;
		background: #fff;
		font-size: 32rpx;
		height: 100%;
		.text-overflow {
			// white-space: nowrap;
			--size:32rpx;
			text-overflow: ellipsis;
			overflow: hidden;
			font-size: var(--size, 16px);
			font-weight: 500;
			color: #445469;
			margin-left: 10rpx;
		}
		.tab {
			line-height: 53px;
			color: #445469;
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 18px;
			font-weight: 500;
			> view {
				border-bottom: 1px solid #f5f7fa;
				flex: 1;
			}
			.active {
				color: #2f80ed;
				border-color: #2f80ed;
			}
		}
		.search-content {
			height: 69px;
			padding: 0 8px 0 14px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.search {
				display: flex;
				align-items: center;
				background: #f5f7fa;
				border-radius: 25px;
				margin-right: 6px;
				padding: 10px 12px;
				box-sizing: border-box;
				height: 44px;
				line-height: 44px;
				flex: 1;
			}
			.option {
				height: 44px;
				line-height: 44px;
				display: flex;
				align-items: center;
				justify-content: space-between;
				// background: #169bd5;
				
				border-radius: 5px;
				width: 100px;
				box-sizing: border-box;
				padding: 0 10px;
				flex-shrink: 0;
				white-space: nowrap;
				view {
					color: #fff;
				}
				
			}
		}
		.scroll-view {
			height: calc(100% - 15px - 54px - 69px);
			border-top: 12px solid #f5f7fa;
			box-sizing: border-box;
			padding: 0 14px;
			.item-content {
				// height: 202px;
				padding: 14px 12px 14px;
				border-bottom: 1px solid #E3E5E8;
				box-sizing: border-box;
				.title {
					// height: 28px;
					line-height: 28px;
					display: flex;
					align-items: center;
					.state {
						border-radius: 0 4px 0 4px;
						font-weight: bold;
						padding: 0 4px;
						line-height: 22px;
						// margin-right: 12px;
						flex-shrink: 0;
						color: #fff;
						font-size: 14px;
						// background: #4c75da;
						&.gray {
							background: #e7ebf1;
							color: #434f60
						}
						&.blue {
							background: #4c75da
						}
						&.orange {
							background: #ff8a2c;
						}
						&.light-blue {
							background: #00a7e4;
						}
						&.red {
							background: #e84a4a;
						}
						&.green {
							background: #45c552;
						}
					}
					.text-overflow {
						flex: 1;
						color: #445469;
						font-weight: bold;
					}
				}
				.date {
					height: 20px;
					line-height: 20px;
					padding: 10px 0 9px;
					color: #6a819e;
					display: flex;
					font-size: 14px;
					align-items: center;
					.date-text {
						margin-left: 3px;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						width: calc(100% - 20px);
					}
				}
				.label {
					line-height: 29px;
					color: #6f747b;
					font-weight: bold;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					font-size: 14px;
				}
				.btn-content {
					display: flex;
					align-items: center;
					font-weight: 500;
					margin-top: 15px;
					text-align: center;
					font-size: 14px;
					> view {
						line-height: 34px;
						width: 100px;
						margin-right: 11px;
						border-radius: 50px;
					}
					.gray {
						color: #445469;
						background: #f5f7fa;
					}
					.blue {
						color: #fff;
						background: #2f80ed;
					}
				}
			}
		}
	}
</style>
